package com.mywebbox.mycraftplugin;

import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;

public class API {

	public static Location Translate(Location l,double x,double y,double z){
		Location l2=l.clone();
		l2.setX(l2.getX()+x);
		l2.setY(l2.getY()+y);
		l2.setZ(l2.getZ()+z);
		
		return l2;
	}
	

	public static Location Rotate(Location l,float pitch,float yaw){
		Location l2=l.clone();
		l2.setPitch(l2.getPitch()+pitch);
		l2.setYaw(l2.getYaw()+yaw);
		
		return l2;
	}
	
	public static boolean isFree (Location l){
		Block b =l.getBlock();
		return(b.getTypeId()==0||b.getTypeId()==70||b.getTypeId()==72||b.getTypeId()==78||b.getTypeId()==27||b.getTypeId()==28||b.getTypeId()==31||b.getTypeId()==32||b.getTypeId()==37||b.getTypeId()==38
				||b.getTypeId()==50||b.getTypeId()==55||b.getTypeId()==59||b.getTypeId()==63||b.getTypeId()==66||b.getTypeId()==68||b.getTypeId()==69||b.getTypeId()==75||b.getTypeId()==76||b.getTypeId()==77
				||b.getTypeId()==83||b.getTypeId()==92||b.getTypeId()==93||b.getTypeId()==94||b.getTypeId()==111||b.getTypeId()==115);
	}
	
	public static boolean isSpawnable(Location l){
		Location l2=Translate(l,0,1,0);	
		Location l3=Translate(l,0,-1,0);
		Block b =l3.getBlock();
		return(isFree(l)&&isFree(l2)&&!isFree(l3)&&b.getTypeId()!=11&&b.getTypeId()!=10&&b.getTypeId()!=51);
	}
	
	public static void Tp(Player p,int max,Location l){
		int somme=(max*3+1);
		int x=0,y=0,z=0;
		for(int i=0;i<max+1;i++){
			for(int j=0;j<max+1;j++){
				for(int k=0;k<max+1;k++){
					if((Math.abs(i)+Math.abs(j)+Math.abs(k))<somme){
						if(isSpawnable(Translate(l,i,j,k))){
							somme=(Math.abs(i)+Math.abs(j)+Math.abs(k));
							x=i;
							y=j;
							z=k;
							
						}
						
						if(isSpawnable(Translate(l,-i,j,k))){
							somme=(Math.abs(i)+Math.abs(j)+Math.abs(k));
							x=-i;
							y=j;
							z=k;
							
						}
						if(isSpawnable(Translate(l,i,-j,k))){
							somme=(Math.abs(i)+Math.abs(j)+Math.abs(k));
							x=i;
							y=-j;
							z=k;
							
						}
						if(isSpawnable(Translate(l,i,j,-k))){
							somme=(Math.abs(i)+Math.abs(j)+Math.abs(k));
							x=i;
							y=j;
							z=-k;
							
						}
						if(isSpawnable(Translate(l,i,-j,-k))){
							somme=(Math.abs(i)+Math.abs(j)+Math.abs(k));
							x=i;
							y=-j;
							z=-k;
							
						}
						if(isSpawnable(Translate(l,-i,j,-k))){
							somme=(Math.abs(i)+Math.abs(j)+Math.abs(k));
							x=-i;
							y=j;
							z=-k;
							
						}
						if(isSpawnable(Translate(l,-i,-j,k))){
							somme=(Math.abs(i)+Math.abs(j)+Math.abs(k));
							x=-i;
							y=-j;
							z=k;
							
						}
						if(isSpawnable(Translate(l,-i,-j,-k))){
							somme=(Math.abs(i)+Math.abs(j)+Math.abs(k));
							x=-i;
							y=-j;
							z=-k;
							
						}
					}
				}
			}
		}
		
		Location l2=Translate(l,x,y,z);
		if(isSpawnable(l2)){
			p.teleport(l2);
			p.sendMessage(ChatColor.GOLD+(I18n._("tp")));
		}
		else{
			p.sendMessage(ChatColor.RED+(I18n._("tpimpossible")));
		}
	}
	
	
}
